/*
 @description: 轮播图上传

 @author: 李建翰

 @update: 2021年9月4日17:06:36
*/

'use strict';
const Controller = require('egg').Controller;
const fs = require('fs');
//const path = require('path');
class RotationChartController extends Controller {
    
    async index() { //搜索
            const {ctx,app} = this;
            // const {keyname} = ctx.request.query;
            const results = await app.mysql.query('select * from rotationchart order by num asc');
            // select ename,sal from emp order by sal asc
            // select * from 表 order by 字段 desc
            ctx.body = {
                code:0,
                msg:"查找成功",
                data:results
            }
        }

        async show(){
            const {ctx,app} = this;
            const {id} = ctx.params;
            const result=await app.mysql.get('rotationchart',{ID:id})
            if(result){
                ctx.body={
                    code:0,
                    msg:'查找成功',
                    data:result
                };
            }else{
                ctx.body = {
                    code:4007,
                    msg:"查找失败"
                }
            }
        }


async create(){      //添加轮播图图片    
    const {ctx,app}=this
    const {action_on,describe_,num}=ctx.request.body
    const count=await app.mysql.query(`select count(*) as count from rotationchart `)
    let nummax=0;
    if(count[0].count!==0){
        if(num){
            const num1=await app.mysql.query(`select num from rotationchart`);
            const result=num1.find(item=>item.num==parseInt(num));
            if(result){
                for(const item of num){
                    if(item.num>=num){
                        const num=item.num+1;
                        const row={
                            num:num
                        }
                        const options={
                            where:{
                                num:item.num
                            }
                        }
                        const data=await app.mysql.update('rotationchart',row,options);
                    }
                }
                nummax=num;
            }else{
                nummax=num;
            }
        }else{
            num.find(function(item){   //查找最大的，如果没有找到，就把当前的最大值加1
                if(item.num>nummax){
                    nummax=item.num;
                }
            });
            nummax+=1;
        }
    }else{
        nummax=1;
    }
    if(action_on && describe_){
        const data=await app.mysql.insert('rotationchart',{action_on,describe_,upload_time:Date.now(),num:nummax});
        ctx.body={
            code:0,
            msg:'插入成功'
        }
    }else{
       ctx.body={
           code:4006,
           msg:"插入失败"
       } 
    }
}

async update(){    //更新轮播图图片
    const {ctx,app}=this
    const {id}=ctx.params
    const {action_on,describe_,num}=ctx.request.body
    const idd=await app.mysql.query(`select id from rotationchart `)
    const result=idd.find(item=>item.id===+id)    //+转换类型
    if(result){
        let row1;
        if(num){
            const numz=await app.mysql.query(`select id from rotationchart where num=${num}`)
            if(numz.length!==0){
                const num1=await app.mysql.query(`select num from rotationchart where id=${id}`)
                const row={
                    num:num1[0].num
                };
                const options={
                    where:{
                        id:numz[0].id
                    }
                };
                const num2=await app.mysql.update('rotationchart',row,options);
            }
            row1={
                action_on,describe_,num,upload_time:Date.now()
            };
        }else{
            row1={
                action_on,describe_,upload_time:Date.now()
            }
        }
        const options1={
            where:{
                id:+id       //转换类型
            }
        };
        // const path = await app.mysql.query(`select action_on from rotationchart where id=${id}`);
        //     fs.unlinkSync('app/'+path[0].action_on);
        const data=await app.mysql.update('rotationchart',row1,options1)

        const path = await app.mysql.query(`select 	action_on from rotationchart where id=${+id}`);
            
            if (path[0].action_on!==action_on) {
                fs.unlink(`app/${path[0].action_on}`, (err) => {
                    if (err) throw err;
                });
            }
        ctx.body={
            code:0,
            msg:'修改成功'
        }
    }else{
        ctx.body={
            code:4007,
            msg:"修改失败"
        }
    }

}
async destroy(){    //删除轮播图图片
    const {ctx,app}=this
    const {id}=ctx.params;
    const result=await app.mysql.delete('rotationchart',{id:+id})
    if(result){
        ctx.body={
            code:0,
            msg:"删除成功"
        };
    }else{
        ctx.body = {
            code: 4008,
            msg: "删除失败",
        };
    }
}
}
module.exports = RotationChartController;